package com.itshizhan.livedatademo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private lateinit var viewModel: MainViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        viewModel = ViewModelProvider(this).get(MainViewModel::class.java)

        resultText.text = viewModel.getResult().toString();

        val resultObserver:Observer<Int> = Observer<Int> {
                result -> resultText.text = result.toString() }

        //观察LiveDada
        viewModel.getResult().observe(this, resultObserver)

        // 求和
        addBtn.setOnClickListener {
            if(number1.text.isNotEmpty()&&number2.text.isNotEmpty()){
                viewModel.add(number1.text.toString().toInt(),number2.text.toString().toInt())
            }else{
                resultText.text = "请正确输入"
            }
        }
    }
}